﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class Post : IEquatable<Post>
    {
        public void ImportList(List<Classes.Post> postList)
        {
            var mdb = new MedregDBDataContext();
            var ListPost = new List<Post>();
            foreach (var post in postList)
            {
                Post post1 = new Post() { ID = post.ID, Name = post.Name, Parent = post.Parent };
                Post query;

                if (mdb.Post.LongCount() == 0)
                {
                    mdb.Post.InsertOnSubmit(post1);
                }
                else
                {
                    try
                    {
                        query = mdb.Post.Single(l => l.ID == post1.ID);
                        query.Name = post1.Name;
                        query.Parent = post1.Parent;
                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {
                        
                        if (!ListPost.Contains(post1))
                        {
                            mdb.Post.InsertOnSubmit(post1);
                            ListPost.Add(post1);
                        }
                    }
                }
            }
            mdb.SubmitChanges();
        }
        public bool Equals(Post other)
        {
            var i = this.ID.Equals(other.ID);
            var j = this.Name.Equals(other.Name);
            if (i & j)
            {
                return true;
            }
            return false;
        }
    }
}
